<?php

class BaseController extends CController
{
    public $isAjax = false;

    public function beforeAction($action)
    {
        return true;        
    }
    
    protected function afterAction()
    {
        $viewName      = Yii::app()->controller->action->id;
        $currentAction = Yii::app()->controller->action;
        
        if ( $currentAction->isReturnUrl )
        {
            Yii::app()->user->setReturnUrl(Yii::app()->request->getRequestUri());    
        }
        
        if ( $currentAction->isRender && ($currentAction->renderPartial || $this->isAjax) )
        {
            $this->renderPartial($viewName, $currentAction->template->getTemplateData(), false, true);        
        }
        else if ( $currentAction->isRender )
        {
            $this->render($viewName, $currentAction->template->getTemplateData());    
        }
    }
    
    protected function registerActions(array $actions)
    {
        $moduleName     = ( Yii::app()->controller->module ) ? 'modules.'.Yii::app()->controller->module->id.'.' : '';
        $controllerName = 'controllers.'.Yii::app()->controller->id.'.';
        
        foreach ( $actions as $actionClass )
        {
            if ( is_array($actionClass) )
            {
                $actionName  = key($actionClass);
                $actionClass = $actionClass[$actionName];
            }
            else
            {
                $actionName  = $actionClass;
                $actionClass = str_replace(' ', '_', ucwords(str_replace('_', ' ', $actionName)));
            }

            $actions[$actionName] = 'application.'.$moduleName.$controllerName.'Action'.$actionClass;
        }

        return $actions;
    }

    public function filters()
    {
        if ( Yii::app()->controller->module && Yii::app()->controller->module->access )
        {
            return array
            (
                'accessControl'
            );
        }

        return array();
    }

    public function accessRules()
    {
        if ( Yii::app()->controller->module && Yii::app()->controller->module->access )
        {
            return array
            (
                array
                (
                    'allow',
                    'roles'  => Yii::app()->controller->module->access
                ),
                array
                (
                    'allow',
                    'actions' => array('login'),
                    'users'   => array('*')
                ),
                array
                (
                    'deny',
                    'users'   => array('*')
                )
            );
        }

        return array();
    }
}